Ext.define('Risk.controller.tree.TreeController',{
	extend : 'Ext.app.Controller',
	stores: [
	         'tree.AreaTreeStore' 
	        ],
	models: [],
	  
	      /**
	       * 视图在congtroller中进行注册
	       * 将控制权转交给 this Controller
	       */
	  	  //管理的视图
     views: [
             'tree.Tree' 
            ],
    id:'treeid',
     url:null,
     params:null,
    // 类创建好后要做的初始化工作
	init : function() {
	  this.control({
		  //类似于JQuery中的选择器
		 'trees':{
		 	celldblclick:function( obj,  td,  cellIndex,  record,  tr,  rowIndex,  e,  eOpts )//用域显示区域信息
		 	{
		 		   var mainTab = obj.up("trees").ownerCt.down('maintab');
				 	if(record.data==undefined||record.data==null||record.data.leaf)return;
				 		mainTab.setDisabled(true);
				 		    //TODO 区域信息显示
				 		var  chart = Ext.create('Risk.view.areaInfo.AreaInfo',{areaId:record.data.id});
				 		     chart.center();
				 		     chart.show();
				 		     
				 		     obj.expandAll( );
		 	
		 	},
			 cellclick:function( obj,  td,  cellIndex,  record,  tr,  rowIndex,  e,  eOpts )//处理具体的项目信息
			 {
			 	   
				 	var mainTab = obj.up("trees").ownerCt.down('maintab');
				 	if(record.data==undefined||record.data==null|| !record.data.leaf)
				 	{
				 		mainTab.setDisabled(true);
				 		return;
				 	}
				 	
				 	//初始化话ra中的TD
				 	//record.data.td(record.data.td);
				 	mainTab.down('rapanel').down('resultfieldSet').down('#td').setValue(record.data.td);
				 	
				 		//初始化所有的panle 加载项设为false
				 		for(var i = 0;i < mainTab.items.length;i++)
				 		{
				 			mainTab.items.get(i).isLoad = false;
				 		}
				 		//maintab 状态为可用状态
				 		mainTab.setDisabled(false);
				 		//激活rapanel
				 		mainTab.setActiveTab(0);
				 		Ext.Ajax.request({
							    url:'ra/get.do' ,
							    params:{projid:record.data.id,_doc:new Date().getMilliseconds()},
							    success:function(response){
							    	
							    var result = Ext.JSON.decode(response.responseText).success;
							    //成功操作form 注入数据
							    //TODO 显示具体详情
							    if(result){
					 				 var  chart = Ext.create('Risk.view.areaInfo.Charts',{projid:record.data.id});
					 		    	 chart.center();
					 		    	 chart.show();
					 		    	 //加载表单中的数据
								     var obj = Ext.JSON.decode(response.responseText).others;
								     mainTab.items.get(0).getForm().setValues(obj);
							    }else//查不到数据 说明没有数据
							    {
							    	mainTab.items.get(0).getForm().reset();
							    }
							   },
							    failure:function(response){
							     // 失败操作from 清空表单中的数据
							    	//mainTab.items.get(0).getForm().reset();
							    }
							});
			 }
		  },
		  'trees  button[action=del]':{
			click:function(obj)
			{
				
				 var model = obj.up('trees').getSelectionModel().selected.items[0] ;
				 if(model==null||model==undefined)
				 {
					  Ext.Msg.alert('Tip', "请选择要删除的信息！");
					  return;
				 }
				 model = model.data;
				 this.params = {id:model.id,_dc:new Date().getMilliseconds()};
				 
				 if(!model.leaf)//删除区域信息
				 {
					 this.url =  'area/del.do';
				 }else//删除项目信息
				 {
					this.url = 'project/delproject.do';
				 }
				 
				 var Store2 = obj.up('trees').store;
				 var res = window.confirm('你确定要删除【'+model.text+"】一旦删除就不能恢复！");
				 if(res)
				 {
					 Ext.Ajax.request({
						    url:this.url ,
						    params:this.params,
						    success:function(response){
						    	 Ext.Msg.alert('Success', "删除成功！");
						    	 Store2.load();
						    },
						    failure:function(response){
						    	 Ext.Msg.alert('Failed', "删除失败！");
						    }
						});
				 }
				 
			}
		},
		'trees  button[action=adda]':{
			 click:function( obj )
			{
				 var model = obj.up('trees').getSelectionModel().selected.items[0] ;
				 var window = null;
				 if(model==undefined || model==null)
				 {   
					 //添加顶级节点
					  window = Ext.create('Risk.view.window.Area',{index:model.index,treeStore1:obj.up('trees').store});  
				 }else
				 {
					 model = model.data;
					 //叶子节点中不能添加区域信息
					 if(model.leaf)
					 {
						 Ext.Msg.alert('Tip', "请选择区域，叶子节点下无法添加区域信息！");
						 return;
					 }
					//添加次级节点
					 window = Ext.create('Risk.view.window.Area',{index:model.index,treeStore1:obj.up('trees').store,pid:model.id,parentname:model.text});
				 }
				 window.center();
				 window.show();
			}
		},
		'trees  button[action=addp]':{
			 click:function(obj)
			{
				 var model = obj.up('trees').getSelectionModel().selected.items[0];
				 if(model==undefined||model==null||model.data.leaf)
				 {
					 Ext.Msg.alert('Tip', "请选择区域！");
					  return;
				 }
				 model = model.data;
			     var window  = Ext.create('Risk.view.window.Project',
			    		   {
				    	        index:model.index,
				    	        treeStore1:obj.up('trees').store,
				    	        pid:model.id,
				    	        parentname:model.text
			    	       });
				 window.center();
				 window.show();
				
			}
		},
		'trees  button[action=update]':
		{
			 click:function(obj)
			{
				 var model = obj.up('trees').getSelectionModel().selected.items[0];
				 if(model==undefined||model==null||model.data.text=='区域信息')
				 {
					 Ext.Msg.alert('Tip', "请选择区域！");
					  return;
				 }
				 
				 
				 if(model.data.leaf)
				 {
				 //修改项目目信息
					 var model = obj.up('trees').getSelectionModel().selected.items[0];
					 if(model==undefined||model==null)
					 {
						 Ext.Msg.alert('Tip', "请选择区域！");
						  return;
					 }
					// model = model.data;
					 var info = {
				 	 				id:model.data.id,
				 	 				parentname:model.data.parentname,
				 	 				pid:model.data.parentId,
				 	 				type:model.data.type,
				 	 				name:model.data.text,
				 	 				objname:model.data.objname
				 	 			};
				     var window  = Ext.create('Risk.view.window.Project',{treeStore1:obj.up('trees').store});
					  window.center();
					  window.show();
					  window.items.get(0).getForm().setValues(info);
					 
				 }else
				 {
				    //修改区域信息
				  	var info = {
				 	 				id:model.data.id,
				 	 				pid:model.data.parentId,
				 	 				light:model.data.td,
				 	 				name:model.data.text,
				 	 				parentname:model.data.parentname
				 	 			};
				    var	window = Ext.create('Risk.view.window.Area',{treeStore1:obj.up('trees').store});
				 		window.center();
				 	    window.show();
				 	    window.items.get(0).getForm().setValues(info);
				 	
				 }
				 
				
			}
			
		}
	  });
	}
});